﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Text;
using EIM_Accounting.Common.DBUtility;//Please add references

namespace Accounting.DAL
{
    /// <summary>
    /// 数据访问类:ContractFee
    /// </summary>
    public partial class ContractFee
    {
        public ContractFee()
        { }

        #region Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQLite.GetMaxID("ID", "ContractFee");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ContractFee");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Accounting.Model.ContractFee model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ContractFee(");
            strSql.Append("ContractID,ActionDate,ActionFee,Remark,ActionType)");
            strSql.Append(" values (");
            strSql.Append("@ContractID,@ActionDate,@ActionFee,@Remark,@ActionType)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ContractID", DbType.Int32,8),
                    new SQLiteParameter("@ActionDate", DbType.String),
                    new SQLiteParameter("@ActionFee", DbType.Decimal,4),
                    new SQLiteParameter("@Remark", DbType.String),
                    new SQLiteParameter("@ActionType", DbType.String)};
            parameters[0].Value = model.ContractID;
            parameters[1].Value = model.ActionDate;
            parameters[2].Value = model.ActionFee;
            parameters[3].Value = model.Remark;
            parameters[4].Value = model.ActionType;

            object obj = DbHelperSQLite.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Accounting.Model.ContractFee model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ContractFee set ");
            strSql.Append("ContractID=@ContractID,");
            strSql.Append("ActionDate=@ActionDate,");
            strSql.Append("ActionFee=@ActionFee,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("ActionType=@ActionType");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ContractID", DbType.Int32,8),
                    new SQLiteParameter("@ActionDate", DbType.String),
                    new SQLiteParameter("@ActionFee", DbType.Decimal,4),
                    new SQLiteParameter("@Remark", DbType.String),
                    new SQLiteParameter("@ActionType", DbType.String),
                    new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = model.ContractID;
            parameters[1].Value = model.ActionDate;
            parameters[2].Value = model.ActionFee;
            parameters[3].Value = model.Remark;
            parameters[4].Value = model.ActionType;
            parameters[5].Value = model.ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ContractFee ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ContractFee ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Accounting.Model.ContractFee GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ContractID,ActionDate,ActionFee,Remark,ActionType from ContractFee ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            Accounting.Model.ContractFee model = new Accounting.Model.ContractFee();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ContractID"] != null && ds.Tables[0].Rows[0]["ContractID"].ToString() != "")
                {
                    model.ContractID = int.Parse(ds.Tables[0].Rows[0]["ContractID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ActionDate"] != null && ds.Tables[0].Rows[0]["ActionDate"].ToString() != "")
                {
                    model.ActionDate = ds.Tables[0].Rows[0]["ActionDate"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ActionFee"] != null && ds.Tables[0].Rows[0]["ActionFee"].ToString() != "")
                {
                    model.ActionFee = decimal.Parse(ds.Tables[0].Rows[0]["ActionFee"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ActionType"] != null && ds.Tables[0].Rows[0]["ActionType"].ToString() != "")
                {
                    model.ActionType = ds.Tables[0].Rows[0]["ActionType"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ContractFee.ID as ID,SignComp,ContractID,ActionDate,ActionFee,ContractFee.Remark as Remark ,ActionType ");
            strSql.Append(" FROM ContractFee, Contract where ContractFee.ContractID = Contract.ID");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            strSql.Append(" order by ContractID,ActionDate,ID");
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "ContractFee";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion Method
    }
}